package com.wzg.ttstestandroid;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.orhanobut.logger.Logger;
import com.wzg.ttstestandroid.RealTimeVoice.RealTimeVoiceUtils;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    /**
     * SpeechUtils.getInstance()
     * 封装了语音识别的精简版
     */

    protected TextView txtResult;
    protected Button btn;
    protected TextView txtLog;

    private void initView() {
        txtResult = findViewById(R.id.txtResult);
        txtLog = findViewById(R.id.txtLog);
        btn = findViewById(R.id.btn);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initPermission();
        RealTimeVoiceUtils.getInstance().init(this);
        btn.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                RealTimeVoiceUtils.getInstance().start(value -> {
                    Toast.makeText(MainActivity.this, "" + value, Toast.LENGTH_SHORT).show();
                    Logger.e("最终结果 : " + value);
                });
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                RealTimeVoiceUtils.getInstance().stop();
            }
            return true;
        });

        findViewById(R.id.btnIntent).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SpeakActivity.class));
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        RealTimeVoiceUtils.getInstance().pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RealTimeVoiceUtils.getInstance().dispose();
    }


    /**
     * android 6.0 以上需要动态申请权限
     */
    private void initPermission() {
        String permissions[] = {Manifest.permission.RECORD_AUDIO,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        };

        ArrayList<String> toApplyList = new ArrayList<String>();

        for (String perm : permissions) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {
                toApplyList.add(perm);
                // 进入到这里代表没有权限.

            }
        }
        String tmpList[] = new String[toApplyList.size()];
        if (!toApplyList.isEmpty()) {
            ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123);
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        // 此处为android 6.0以上动态授权的回调，用户自行实现。
    }
}